<template>
	  <div>
		    <el-container>
			      <el-header>
				        <router-link :to="{name:'Blogs'}">
					        <img src="../assets/VueBlog-logo.png"   style="height: 60%; margin-top: 10px;">
					        </router-link>
				      </el-header>
			      <el-main>
				        <el-form :model="ruleForm" status-icon :rules="rules" ref="loginForm" label-width="100px"                
				  class="demo-ruleForm">
					<h1>登录</h1>
					          <el-form-item label="用户名" prop="username">
						            <el-input type="text" maxlength="12" v-model="ruleForm.username"></el-input>
						          </el-form-item>
					          <el-form-item label="密码" prop="password">
						            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
						          </el-form-item>
					          <el-form-item>
						            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
						            <el-button @click="resetForm('loginForm')">重置</el-button>
						<el-button type="warning" @click="register">注册</el-button>
					</el-form-item>
					       
				</el-form>
				      </el-main>
			    </el-container>
		  </div>
</template>
<script>
	export default {
		name: 'Login',
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					password: '',
					username: ''
				},
				rules: {
					password: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					username: [{
							required: true, //是否必填
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 12,
							message: '长度在 3 到 12 个字符',
							trigger: 'blur'
						}
					]
				}
			};
		},
		methods: {
			submitForm(formName) {
				const _this = this;
				this.$refs[formName].validate((valid) => {
					if (valid) {
						// 提交逻辑
						this.$axios.post('/login', this.ruleForm).then((res) => {
							// console.log(res)
							const token = res.headers['authorization']
							_this.$store.commit('SET_TOKEN', token)
							_this.$store.commit('SET_USERINFO', res.data.data)
							_this.$router.push("/blogs")
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},
			register() {
				this.$router.push('/register');
			}
		},
		mounted() {

		}
	}
</script>

<style scoped>
	.el-header,
	.el-footer {
		background-color: #B3C0D1;
		color: #333;
		text-align: center;
		line-height: 60px;
	}

	/* .el-aside {
		background-color: #D3DCE6;
		color: #333;
		text-align: center;
		line-height: 200px;
	} */

	.el-main {
		/* background-color: #E9EEF3; */
		color: #333;
		text-align: center;
		line-height: 20px;
	}

	body>.el-container {
		margin-bottom: 40px;
	}

	/* .el-container:nth-child(5) .el-aside,
	.el-container:nth-child(6) .el-aside {
		line-height: 260px;
	} 

	.el-container:nth-child(7) .el-aside {
		line-height: 320px;
	} */

	.mlogo {
		height: 60%;
		margin-top: 10px;
	}

	.demo-ruleForm {
		max-width: 500px;
		margin: 0 auto;
	}
</style>
